<?php
/**
 * User: aka
 * Time: 1:26
 */

namespace app\common;

use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Session;

class Email
{
    private $email;
    private $password;
    private $qqHost;
    private $port;
    private $SMTPSecure;
    private $qqAccount;
    private $emailTitle;
    private $sendName;
    private $emailCode;

    public function __construct()
    {
        $this->email = new PHPMailer();
        $this->password = 'hzvlwgkdfwljcagg';
        $this->qqHost = 'smtp.qq.com';
        $this->port = '465';
        $this->SMTPSecure = 'ssl';
        $this->qqAccount = '425084984@qq.com';
        $this->emailTitle = config('app.app_name').' - 请接收您的验证码';
        $this->sendName = 'LONG57';
        $this->emailCode = get_rand_num();
    }

    public function __destruct()
    {
        unset(
            $this->email,
            $this->password,
            $this->qqHost,
            $this->port,
            $this->SMTPSecure,
            $this->qqAccount,
            $this->emailTitle,
            $this->sendName,
            $this->emailCode
        );
    }

    private function getEmailContent()
    {
        $content = "您的验证码为：<b style='font-size: 30px;color: red;'>".$this->emailCode."</b>";
        return $content;
    }

    public function setEmailConfigAndSendForgetPasswordEmail(string $email_address)
    {
        $this->email->isSMTP();
        $this->email->CharSet = 'UTF-8';
        $this->email->Host = $this->qqHost;
        $this->email->Port = $this->port;
        $this->email->SMTPAuth = true;
        $this->email->SMTPSecure = $this->SMTPSecure;
        $this->email->Username = $this->qqAccount;
        $this->email->Password = $this->password;
        $this->email->From = $this->qqAccount;
        $this->email->FromName = $this->sendName;
        $this->email->Subject = $this->emailTitle;
        $this->email->addAddress($email_address);
        $this->email->isHTML(true);
        $this->email->Body = $this->getEmailContent();
        return $this->email->send() ?
            $this->saveEmailDataAndSession($email_address)
            :
            json($this->email->ErrorInfo);
    }

    private function saveEmailDataAndSession(string $email_address)
    {
        \app\tenno\model\Email::create([
            'receive_account' => $email_address,
            'send_email_address' => $this->qqAccount,
            'send_code' => $this->emailCode,
            'delete_state' => 0
        ]);
        Session::set('forget_password_email_code', $this->emailCode);
        Session::set('forget_password_email_address', $email_address);
        return json(['state' => 200]);
    }

    public function setEmailConfigAndSendEmail(string $email_address)
    {
        $this->email->isSMTP();
        $this->email->CharSet = 'UTF-8';
        $this->email->Host = $this->qqHost;
        $this->email->Port = $this->port;
        $this->email->SMTPAuth = true;
        $this->email->SMTPSecure = $this->SMTPSecure;
        $this->email->Username = $this->qqAccount;
        $this->email->Password = $this->password;
        $this->email->From = $this->qqAccount;
        $this->email->FromName = $this->sendName;
        $this->email->Subject = $this->emailTitle;
        $this->email->addAddress($email_address);
        $this->email->isHTML(true);
        $this->email->Body = $this->getEmailContent();
        return $this->email->send() ?
            $this->saveEmailData($email_address)
            :
            json($this->email->ErrorInfo);
    }

    private function saveEmailData(string $email_address)
    {
        \app\tenno\model\Email::create([
            'receive_account' => $email_address,
            'send_email_address' => $this->qqAccount,
            'send_code' => $this->emailCode,
            'delete_state' => 0
        ]);
        return json(['state' => 200, 'email_code' => $this->emailCode]);
    }
}